package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IAppointmentService appointmentService;         // 预约单业务

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        // 创建时间
        statement.setCreateTime(new Date());
        // 默认消费中状态
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        // 消费中的预约单才能修改
        Statement temp = this.get(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(temp.getStatus())) {
            throw new BusinessException("该状态不为消费中,无法进行修改");
        }

        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);

        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountPrice) {

        statementMapper.updateAmount(statementId, totalAmount, totalQuantity, discountPrice);
    }


    /**
     * 预约单跳转结算单
     * @param appointmentId
     * @return
     */
    @Override
    public Statement generateStatement(Long appointmentId) {

        // 查询预约单
        Appointment appointment = appointmentService.get(appointmentId);
        // 添加结算单
        Statement statement = new Statement();

        statement.setAppointmentId(appointment.getId());                    // 预约单ID
        statement.setCustomerName(appointment.getCustomerName());           // 客户姓名
        statement.setCustomerPhone(appointment.getCustomerPhone());         // 联系方式
        statement.setActualArrivalTime(appointment.getActualArrivalTime()); // 到店时间
        statement.setLicensePlate(appointment.getLicensePlate());           // 车牌号码
        statement.setCarSeries(appointment.getCarSeries());                 // 汽车类型
        statement.setServiceType(appointment.getServiceType().longValue());// 服务类型

        statementMapper.insert(statement);

        return statement;
    }

    // 预约单跳转结算单
    @Override
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.selectByAppointmentId(appointmentId);
    }


    /**
     * 确认支付
     * @param statementId
     */
    @Override
    public void payStatement(Long statementId) {

        /*// 支付成功
        Statement statement = this.get(statementId);
        if (statement == null) {
            // 结算单->收款状态-收款时间-收款人
            // 预约的结算单
        } else {
            throw new BusinessException("参数异常");
        }*/



        // 支付成功
        Statement statement = this.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("结算单已经支付无需再进行支付");
        }
        // 结算单->收款状态-收款时间-收款人
        statement.setStatus(statement.getStatus());
        statement.setCreateTime(new Date());
        Long userId = ShiroUtils.getUserId();   // 当前登录用户
        statement.setPayeeId(userId);           // 收款人
        statement.setStatus(Statement.STATUS_PAID);
        // 预约的结算单

        // 更新结算单
        statementMapper.updatePayStatus(statementId,statement.getStatus(), new Date(), ShiroUtils.getUserId());

        // 如果是预约的结算单,修改预约单的状态
        if (statement.getAppointmentId() != null) {
            appointmentService.changStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }
    }


    /**
     * 服务结算单中的删除按钮操作
     * @param id
     */
    @Override
    public void delete(Long id) {

        Statement statement = this.get(id);
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {

            statementMapper.deleteByPrimaryKey(id);
            statementItemService.deleteByStatementId(id);

            // 如果删除可以将预约记录改为用户取消
//            Long appointmentId = statement.getAppointmentId();
//            appointment.setStatus(Appointment.STATUS_CANCEL);
            appointmentService.updateAppointmentStatus(statement.getAppointmentId(),Appointment.STATUS_CANCEL);
        } else {
            throw new BusinessException("只有在消费中才允许删除");
        }
    }



}
